package com.cloudbroker.bcs.platform.dav.middle.bizservice.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.cloudbroker.bcs.common.entity.HSBCPSBackVO;
import com.cloudbroker.bcs.common.util.StringUtil;
import com.cloudbroker.bcs.common.util.VOUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes.SysMgtErr;
import com.cloudbroker.bcs.platform.dav.bizapi.service.base.vo.HSBCPSVO;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.common.CommonBizService;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.common.back.CommonReplyList;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.common.vo.CommonReplyIdForm;
import com.cloudbroker.bcs.platform.dav.bizapi.service.business.common.vo.CommonReplySaveForm;
import com.cloudbroker.bcs.platform.dav.middle.context.HSBCPSBizDataContext;
import com.cloudbroker.bcs.platform.dav.middle.entity.CommonReply;
import com.cloudbroker.bcs.platform.dav.middle.entityview.CommonReplyView;
import com.cloudbroker.bcs.platform.dav.middle.service.CommonReplyService;
import com.cloudbroker.bcs.platform.dav.middle.service.CustomerService;
import com.cloudbroker.bcs.platform.dav.middle.service.PayWaterService;

@Service
public class CommonBizServiceImpl implements CommonBizService {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(CommonBizServiceImpl.class);

	@Resource
	private PayWaterService payWaterService;
	@Resource
	private CustomerService customerService;
	@Resource
	private CommonReplyService commonReplyService;
	
    @Override
    public CommonReplyList funcCommonReplyList(HSBCPSVO param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        List<CommonReplyView> list = this.commonReplyService.getByBrokerId(broker_id);
        CommonReplyList backList = new CommonReplyList();
        if(null != list && list.size() > 0){
            backList.newDataList().addAll(list);
        }
        return VOUtil.setSuccessResult(backList);
    }

    @Override
    public HSBCPSBackVO funcCommonReplySave(CommonReplySaveForm param) {
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        String common_reply_id = param.getCommon_reply_id();
        String reply_content = param.getReply_content();
        //新增
        if(StringUtil.isEmpty(common_reply_id)){
            //限制条数
            Integer count = this.commonReplyService.getCount(broker_id);
            if(count >= 10){
                return VOUtil.genErrorResult(SysMgtErr.COMMON_REPLY_LIMIT,HSBCPSBackVO.class);
            }
            CommonReply cr = new CommonReply();
            cr.setBroker_id(broker_id);
            cr.setReply_content(reply_content);
            cr.setCreate_time(new Date());
            cr.setUpdate_time(new Date());
            this.commonReplyService.save(cr);
        }else{
            //修改
            CommonReply cr = this.commonReplyService.get(common_reply_id);
            if(null == cr){
                return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_COMMON_REPLY_ID,HSBCPSBackVO.class);
            }
            if(!cr.getBroker_id().equals(broker_id)){
                return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_COMMON_REPLY_ID,HSBCPSBackVO.class);
            }
            cr.setReply_content(reply_content);
            cr.setUpdate_time(new Date());
            this.commonReplyService.update(cr);
        }
        return VOUtil.genSuccessBackVo();
    }

    @Override
    public HSBCPSBackVO funcCommonReplyDel(CommonReplyIdForm param) {
        String common_reply_id = param.getCommon_reply_id();
        Long broker_id = HSBCPSBizDataContext.getBrokerId();
        CommonReply cr = this.commonReplyService.get(common_reply_id);
        if(null != cr){
            if(!cr.getBroker_id().equals(broker_id)){
                return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_COMMON_REPLY_ID,HSBCPSBackVO.class);
            }
            commonReplyService.delete(common_reply_id);
        }else{
            return VOUtil.genErrorResult(SysMgtErr.NO_SUCH_COMMON_REPLY_ID,HSBCPSBackVO.class);
        }
        return VOUtil.genSuccessBackVo();
    }
}